<?php
	global $SSO, $Security, $AppID, $User, $DB;
	//Check to see if there is a valid session 
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
		
	//Check to see if the user has rights to be on LivingWeb
	$PageRights = array('ADMIN','USER');
	$UserRoles = $Security->ValidateUser($SessionInfo['username'], $AppID, $PageRights, 1);
	
	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);	

	if(isset($_POST['PID']))
	{
		global $DB;
		
		$show_nav = null;
		if(isset($_POST['chbShowNav']))
		{
			$show_nav = 1;
		}
		
		$db_string = $DB->compile_db_update_string( 
		array (
			'PageTitle'		=> $_POST['txtPageTitle'],
			'MinistryID'   		=> $_POST['txtMinistryID'],
			'LastEditDate'    		=> date("Y-m-d H:i:s"),
			'LastEditBy'		=> $SessionInfo['username'],
			'DisplayNavigation' => $show_nav)
			);
		
		$DB->query("UPDATE pages SET " . $db_string . " WHERE PageID = '" . $_POST['PID'] . "'");
		
		header("Location: index.php?action=edit&PID=" . $_POST['PID']);
	}
	if(isset($_GET['PID']))
	{
		global $DB;
		$DB->query("SELECT * FROM pages p JOIN ministries m ON p.MinistryID = m.MinistryID JOIN page_content pc ON p.PageID = pc.PageID WHERE p.PageID = '" . $_GET['PID'] . "'");
		if(!($row = $DB->fetch_row()))
		{	
			print 'There was an unexpected error.';
			exit();
		}
		
		if(isset($_GET['main_page']) && $_GET['main_page'] == 'yes')
		{
			$DB->query("update pages set MainPage = '1' where PageID = '" . $_GET['PID'] . "'");
			$DB->query("update pages set MainPage = '0' where PageID <> '" . $_GET['PID'] . "' and MinistryID = '" . $row['MinistryID'] . "'");
			
			header("Location: index.php?action=edit&edit_action=edit_props&PID=".$_GET['PID']);
		}
	}
?>
<form name="frmEditProps" action="index.php?action=edit_props" method="post">
<table cellpadding="0" cellspacing="2" border="0" style="border:1px solid #D8DA85;background:#FEFFCD;" width="100%">
	<tr>
		<td valign="top">
			<table cellpadding="3" cellspacing="0" style="border-bottom:3px double #D8DA85;" width="100%" border="0">
				<tr>
					<td colspan="3"><span class="TB1">Page Properties</span></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td colspan="3" align="center">
			<table cellpadding="0" cellspacing="0" width="95%" border="0">
				<tr>	
					<td colspan="3" nowrap><span class="T1"><em>Title: </em><input name="txtPageTitle" type="text" class="T1" size="50" value="<?php print $row['PageTitle']; ?>">&nbsp;&nbsp;&nbsp;<em>(PID <?php print $row['PageID']; ?>)</em></span></td>
				</tr>
				<tr>
					<td nowrap><span class="T1"><em>Ministry:</em> <?php print $row['MinistryName']; ?><!--<select name="selMinistryID" class="T1"><option value="MUSICMIN">Music</option></select>--></span>
					&nbsp;&nbsp;<span class="TB1" style="color:#FF0000;"><?php if($row['MainPage'] == 1) {print "Main Page";}else{print '<input value="Make Main Page" type="button" onClick="location.href=\'index.php?action=edit&edit_action=edit_props&main_page=yes&PID='.$_GET['PID'].'\';" class="B">';} ?></span></td>
				</tr>
        <tr><td nowrap><span class="T1"><em>Show Navigation?</em></span><input name="chbShowNav" type="checkbox" <?php if($row['DisplayNavigation'] == 1) { print "checked"; } ?>></td></tr>
        <tr><td nowrap align="right"><input class="B" name="btnSave" value="Save" type="submit"></td></tr>
			</table>
		</td>
	</tr>
</table>
<input type="hidden" value="<?php print $row['PageID']; ?>" name="PID">
<input type="hidden" value="<?php print $row['MinistryID']; ?>" name="txtMinistryID">
</form>